package com.example.opengldemo.simple13_01;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorEventListener2;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.opengldemo.R;

public class PROXIMITYActivity extends AppCompatActivity {
    TextView text;
    TextView textX;
    TextView textY;
    TextView textZ;
    SensorManager manager;

    private SensorEventListener mySensorListener2 = new SensorEventListener2() {

        @Override
        public void onSensorChanged(SensorEvent event) {
            float []values=event.values;
            textX.setText("距离手机： "+values[0]);
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }

        @Override
        public void onFlushCompleted(Sensor sensor) {

        }
    } ;
    Sensor myAccelerometer ;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sensor);
        text = findViewById(R.id.text);
        textX = findViewById(R.id.textX);
        textY = findViewById(R.id.textY);
        textZ = findViewById(R.id.textZ);

        manager = (SensorManager) getSystemService(SENSOR_SERVICE);

    }

    @Override
    protected void onStart() {
        super.onStart();
        myAccelerometer = manager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
        StringBuffer strb = new StringBuffer();
        strb.append("\n 名称: ");
        strb.append(myAccelerometer.getName());
        strb.append("\n 耗电量(mA): ");
        strb.append(myAccelerometer.getPower());
        strb.append("\n 类型编号 : ");
        strb.append(myAccelerometer.getType());
        strb.append("\n 制造商: ");
        strb.append(myAccelerometer.getVendor());
        strb.append("\n 版本: ");
        strb.append(myAccelerometer.getVersion());
        strb.append("\n 最大测量范围: ");
        strb.append(myAccelerometer.getMaximumRange());
        text.setText(strb.toString());
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(mySensorListener2 != null && myAccelerometer != null)
            manager.registerListener(mySensorListener2 , myAccelerometer ,  SensorManager.SENSOR_DELAY_NORMAL) ;
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(mySensorListener2 != null && myAccelerometer != null)
            manager.unregisterListener(mySensorListener2 , myAccelerometer ); ;

    }
}
